package fsoft.com.vn.directory.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import fsoft.com.vn.directory.form.GovOfficeRegion;

@Repository
public class GovOfficeRegionDAOImpl implements GovOfficeRegionDAO {

	@Autowired
	private SessionFactory sessionFactory;

	public ArrayList listGov(int page, String split, int check) {
		String checksql = "";
		String WHERE = "";
		if(split.equals("ABCDE")){
			WHERE = "WHERE name LIKE 'A%' OR  name LIKE 'B%' OR  name LIKE 'C%' OR  name LIKE 'D%' OR  name LIKE 'E%'";
		} else if(split.equals("FGHIJ")){
			WHERE = "WHERE name LIKE 'F%' OR  name LIKE 'G%' OR  name LIKE 'H%' OR  name LIKE 'I%' OR  name LIKE 'J%'";
		}else if(split.equals("KLMN")){
			WHERE = "WHERE name LIKE 'K%' OR  name LIKE 'L%' OR  name LIKE 'M%' OR  name LIKE 'N%'";
		}else if(split.equals("OPQR")){
			WHERE = "WHERE name LIKE 'O%' OR  name LIKE 'P%' OR  name LIKE 'Q%' OR  name LIKE 'R%'";
		}else if(split.equals("STUV")){
			WHERE = "WHERE name LIKE 'S%' OR  name LIKE 'T%' OR  name LIKE 'U%' OR  name LIKE 'V%'";
		}else if(split.equals("WXYZ")){
			WHERE = "WHERE name LIKE 'W%' OR  name LIKE 'X%' OR  name LIKE 'Y%' OR  name LIKE 'Z%'";
		}
		if(check == 0 && !WHERE.equals("")) checksql = " AND isactive like '1' ";
		if(check == 0 && WHERE.equals("")) checksql = " WHERE isactive like '1' ";
		Query q = sessionFactory.getCurrentSession().createQuery("from GovOfficeRegion " + WHERE +" "+checksql+" ");
		ArrayList xxx = new ArrayList();
		xxx.add((int)Math.ceil((double)q.list().size()/3));
		q.setMaxResults(3);
		if(page > 0){
			q.setFirstResult(((page-1)*3));
		}
		xxx.add(q.list());
		return xxx;
	}

	public GovOfficeRegion viewDetail(String id) {

		Query q = sessionFactory.getCurrentSession().createQuery(
				"from GovOfficeRegion where id = '" + id + "'");
		return (GovOfficeRegion) q.uniqueResult();

	}
}
